import $Util from "@/common/util.js";
//管理创建商品的状态
export default {
	state: {
		goods_id: 0, //正在修改的商品ID
		
		sku_type: 0,
		//form表单
		title: '',
		category: [],
		desc:'',
		unit:'件',
		stock:0,
		min_stock:0,
		display_stock:0,
		status:0,
		express:'',
		
		oprice:0, //市场价格
		pprice:0, //销售价格
		cprice:0, //成本价格
		weight:0, //重量
		volume:0, //体积
		
		banners: [], //轮播图
		
		//规格卡片
		/**
		 * 数组，
		 * 数组里面对象
		 * {
			name: '颜色',
			type:0, //0表示只有文字 1 颜色 2图片
			list:[
				{
					name: '黄色',
					image: '',
					color: ''
				},
				{
					name: '红色',
					image: '',
					color: ''
				}
			]
		}
		 */
		sku_card:[],
		//商品类型
		goods_type_id: '',
		//商品属性
		goods_attrs: {
			phone_model: ''
		},
		
		//折扣
		discount: 0,
		
		// sku组合情况表头
		ths: [
			{name: '商品规格', rowspan: 1, colspan:1, width: ''},
			{name: 'sku图片', rowspan: 2, width: '60'},
			{name: '销售价', rowspan: 2, width: '100'},
			{name: '市场价', rowspan: 2, width: '100'},
			{name: '成本价', rowspan: 2, width: '100'},
			{name: '库存', rowspan: 2, width: '100'},
			{name: '体积', rowspan: 2, width: '100'},
			{name: '重量', rowspan: 2, width: '100'},
			{name: '编码', rowspan: 2, width: '100'},
		],
		
	},
	
	getters: {
		skuLabels(state) {
			return state.sku_card.filter(v => v.list.length > 0)
		},
		//获取表头
		tableThs(state, getters) {
			let len = getters.skuLabels.length
			state.ths[0].colspan = len
			state.ths[0].rowspan = len > 0 ? 1 : 2
			return state.ths
		},
		//获取表格数据
		tableData(state) {
			//当前是否有规格卡片
			if (state.sku_card.length === 0) return []
			let sku_list = []
			for (let i = 0; i < state.sku_card.length; i++) {
				if (state.sku_card[i].list.length > 0) {
					sku_list.push(state.sku_card[i].list)
				}
			}
			if(sku_list.length === 0) return []
			let arr = $Util.cartesianProductOf(...sku_list)
			return arr.map(v => {
				let obj = {
					skus: [],
					image: '',
					pprice:0,
					oprice:0,
					cprice:0,
					stock:0,
					volume:0,
					weight:0,
					code:''
				}
				obj.skus = v
				return obj
			})
		}
	},
	
	mutations: {
		// 修改state
		vModelState(state, {key,value}) {
			state[key] = value
		},
		
		//增加规格卡片
		addSkuCard(state, data) {
			state.sku_card.push(data)
		},
		
		//删除规格卡片
		delSkuCard(state, index) {
			state.sku_card.splice(index, 1)
		},
		
		//修改卡片
		vModelSkuCard(state, {key, index, value}) {
			state.sku_card[index][key] = value
		},
		
		//修改商品属性
		vModelGoodsAttrs(state, {key, value}) {
			state.goods_attrs[key] = value
		},
		
		//卡片排序
		sortSkuCard(state,{action, index}) {
			//上移
			$Util[action](state.sku_card, index)
		},
		
		//增加规格属性
		addSkuValue(state, {index, data}) {
			
			state.sku_card[index].list.push(data)
		},
		
		//删除指定规格属性 cardIndex规格卡片索引； specValueIndex具体一个规格值的索引
		delSkuValue(state, {cardIndex, specValueIndex}) {
			state.sku_card[cardIndex].list.splice(specValueIndex, 1)
		},
		
		//修改指定规格卡片的规格值
		updateSkuValue(state, {cardIndex, specValueIndex, key, value}) {
			state.sku_card[cardIndex].list[specValueIndex][key] = value
		},
		
		//排序规格卡片的规格值次序
		sortSkuValue(state, {index,value}) {
			state.sku_card[index].list = value
		}
	},
	
	actions: {
		
	}
}